package com.xxcxcxcx.jobsystem.common.Job.Farmer;

import com.xxcxcxcx.jobsystem.PlayerSaveData;
import com.xxcxcxcx.jobsystem.common.Job.JobBase;
import com.xxcxcxcx.jobsystem.common.JobsClass;
import com.xxcxcxcx.jobsystem.common.capability.JobCapability;
import com.xxcxcxcx.jobsystem.common.capability.JobCapabilityProvider;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.BonemealableBlock;
import net.minecraft.world.level.block.CropBlock;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.common.util.LazyOptional;

public class Farmer extends JobBase {
    public static String Job = JobsClass.Farmer;
    public static int getLootCount(Player player, BlockState blockState, Level level, BlockPos blockPos){
        LazyOptional<JobCapability> capability = JobCapabilityProvider.getPlayerData(player);
        if(!capability.isPresent()) {
            return 1;
        }
        PlayerSaveData playerSaveData = capability.orElse(null);
        int num = 1;
        if(blockState.getBlock() instanceof CropBlock crop && crop.isMaxAge(blockState)){
            int farmlevel =playerSaveData.getLevel(Job);
            if(farmlevel>=7&&player.getRandom().nextDouble()<(farmlevel*0.05)){
                blockState.getBlock().popExperience((ServerLevel) level, blockPos, player.getRandom().nextInt(0,farmlevel-7+1));
            }
            num += GetUP(player);
            if(num<1){
                level.setBlock(blockPos, crop.getStateForAge(crop.getMaxAge()-player.getRandom().nextInt(1,Math.min(4-farmlevel,crop.getMaxAge()))), 2);
            }
            UpExperience(player,(Math.max(num, 1))*5, Job);
        }
        return num;
    }
    public static int getBonemealCount(Player player, BlockState blockState, Level level, BlockPos blockPos){
        LazyOptional<JobCapability> capability = JobCapabilityProvider.getPlayerData(player);
        if(!capability.isPresent()) {
            return 1;
        }
        PlayerSaveData playerSaveData = capability.orElse(null);
        int num = 1;
        if(blockState.getBlock() instanceof BonemealableBlock Bonemeal && Bonemeal.isValidBonemealTarget(level,blockPos,blockState,false)){
            int farmlevel =playerSaveData.getLevel(Job);
            num += GetUP(player);
            UpExperience(player,(Math.max(num, 1))*25,Job);
        }
        return num;
    }
    private static int GetUP(Player player){
        LazyOptional<JobCapability> capability = JobCapabilityProvider.getPlayerData(player);
        if(!capability.isPresent()) {
            return 0;
        }
        PlayerSaveData playerSaveData = capability.orElse(null);
        int farmlevel =playerSaveData.getLevel(Job);
        int num=0;
        if(farmlevel<4){
            num -= (player.getRandom().nextDouble()<(0.5 - 0.125 * farmlevel)?1:0);
        }
        if(farmlevel>=4){
            num += (player.getRandom().nextDouble()<(0.25 * (farmlevel-3))?1:0);
        }
        if(farmlevel>=7){
            num += (player.getRandom().nextDouble()<(0.10 * farmlevel)?1:0);
        }
        if(farmlevel>=10){
            num += (player.getRandom().nextDouble()<(0.05 * farmlevel)?1:0);
        }
        return num;
    }

    public static boolean Farmland(Entity entity) {
        if(entity instanceof Player player){
            LazyOptional<JobCapability> capability = JobCapabilityProvider.getPlayerData(player);
            if(!capability.isPresent()) {
                return false;
            }
            PlayerSaveData playerSaveData = capability.orElse(null);
            return playerSaveData.getLevel(Job) >= 7;
        }
        return false;
    }

    public static void FarmFeedFood(ServerPlayer sp, InteractionHand hand, LivingEntity target) {
        LazyOptional<JobCapability> capability = JobCapabilityProvider.getPlayerData(sp);
        if(!capability.isPresent()) {
            return;
        }
        PlayerSaveData playerSaveData = capability.orElse(null);
        int farmlevel =playerSaveData.getLevel(Job);
        if(farmlevel!=10)return;
        if(playerSaveData.getExperienceCD(Job)!=0)return;
        ItemStack itemStack = sp.getItemInHand(hand);
        if(itemStack.getFoodProperties(target)==null)return;
        playerSaveData.setExperienceCD(Job,1200);
        target.eat(sp.level,itemStack);
    }
}
